<?php

class ActionsController extends AppController {
    
    public $uses = array('Action','ActionPost','Group','Place');
    
    const PROPAGATION_GROUP = 'group';
    const PROPAGATION_CITY = 'city';
    const PROPAGATION_DEPARTMENT = 'department';
    const PROPAGATION_STATE = 'state';
    const PROPAGATION_COUNTRY = 'country';
    const PROPAGATION_WORLD = 'world';
    
    public static $places = array (self::PROPAGATION_GROUP,self::PROPAGATION_CITY,self::PROPAGATION_DEPARTMENT,self::PROPAGATION_STATE,self::PROPAGATION_COUNTRY);
    
    ////////////////////////////////////////////////////////////
    public function addAction($propagationMax = self::PROPAGATION_GROUP){
        
        $user = $this->getCurrentUser();
        $group = $this->getCurrentUserGroup();
        
        if ($propagationMax == self::PROPAGATION_GROUP){
            $place_id_Max = 0;
        }
        else if ($propagationMax == self::PROPAGATION_CITY){
            $place_id_Max = $group['Group']['cities_id'];  
        }
        else if ($propagationMax == self::PROPAGATION_DEPARTMENT){
            $place_id_Max = $this->getPlacesIdMax($group,self::PROPAGATION_DEPARTMENT);
        }
        else if ($propagationMax == self::PROPAGATION_STATE){
            $place_id_Max = $this->getPlacesIdMax($group,self::PROPAGATION_STATE);
        }        
        else if ($propagationMax == self::PROPAGATION_COUNTRY){
            $place_id_Max = $this->getPlacesIdMax($group,self::PROPAGATION_COUNTRY);
        }
        else if ($propagationMax == self::PROPAGATION_WORLD){
            $place_id_Max = 1;
        }
        else 
            echo 'Mauvais !';
      
        $idUser = $this->getCurrentUserId();
        
        $data = array('Action' => array(
            'groups_id' => $group['Group']['id'],
            'users_id' => $idUser,
            'places_id' => $group['Group']['cities_id'],
            'propagationMax' => $propagationMax,
            'place_id_max' => $place_id_Max,
            'propagationCurrent' => self::PROPAGATION_GROUP,
            'created_on' => date('c')
        ));
        $data = $this->Action->save($data);
        
        return $data;
    }
    
    ////////////////////////////////////////////////////////////
    public function addActionPost(){
        if (!empty($this->request->data['ActionPost'])){
            $info = $this->addAction('group');
            
            $data  = $this->request->data['ActionPost'];
            $data['actions_id'] = $info['Action']['id'];
            $this->ActionPost->save($data);
            $this->Session->setFlash('Votre post a été enregistré');
            
        }
        $this->redirect(array('controller' => 'Groups', 'action' => 'home'));
    }
    
    ////////////////////////////////////////////////////////////
      /*
     * @param $id -> Id d'une place
     * @param $where
     * @return liste de place 
     * 
     * Exemple :
     * $id = Isère
     * $where = PlaceDepartment
     * retourne la liste des ville de l'isère
     * 
    */
    public function search($id,$where = array(
        'PlaceCountry','PlaceState', 'PlaceDepartment', 'World'))
    {
        if ($where == 'World') {
            $data = $this->PlaceCountry->find('all');
        }
        else if($where == 'PlaceCountry'){
            $data = $this->PlaceState->find('all',array(
                'conditions' => array(
                    'PlaceState.countries_id' => $id
                )
            ));
        }
        else if($where == 'PlaceState'){
            $data = $this->PlaceDepartment->find('all',array(
                'conditions' => array(
                    'PlaceDepartment.states_id' => $id
                )
            ));
        }
        else if ($where == 'PlaceDepartment'){
            $data = $this->PlaceCity->find('all',array(
                'conditions' => array(
                    'PlaceCity.departments_id' => $id
                )
            ));
        }
        else 
            $data = NULL;
        
        return $data;
    }
    
    
    ////////////////////////////////////////////////////////////
    public function getPlacesIdMax($group, $where = array(
        'country','state', 'department', 'city'
    )){
        $idPlaceCity = $group['Group']['cities_id'];
        
        $City = $this->Place->PlaceCity->find('first',array(
            'fields' => array( 
                'PlaceCity.departments_id'
            ),
            'conditions' => array (
                'PlaceCity.places_id' => $idPlaceCity
            )
        ));
    
        $idPlaceDep = $City['PlaceCity']['departments_id'];
        
        $Dep = $this->Place->PlaceDepartment->find('first',array(
            'fields' => array( 
                'PlaceDepartment.states_id'
            ),
            'conditions' => array (
                'PlaceDepartment.places_id' => $idPlaceDep
            )
        ));
        
        $idPlaceState = $Dep['PlaceDepartment']['states_id'];
              
        $State = $this->Place->PlaceState->find('first',array(
            'fields' => array( 
                'PlaceState.countries_id'
            ),
            'conditions' => array (
                'PlaceState.places_id' => $idPlaceState
            )
        ));
        
        $idPlaceCountry = $State['PlaceState']['countries_id'];
        
         $Country = $this->Place->PlaceCountry->find('first',array(
            'fields' => array( 
                'PlaceCountry.places_id'
            ),
            'conditions' => array (
                'PlaceCountry.places_id' => $idPlaceCountry
            )
        ));
        
        if($where == 'country'){
            $data = $Country['PlaceCountry']['places_id'];
        }
        else if($where == 'state'){
            $data = $idPlaceState;
        }
        else if ($where == 'department'){
            $data = $idPlaceDep;
        }
        else if ($where == 'city') {
            $data = $idPlaceCity;
        }
        else 
            $data = NULL;
      
        return $data;   
    }   
    
    /////////////////////////////////////////////////////////////
    /*
        $action : mon action
        $group : groupe duquel par l'action
        @return la liste des id des groupes dans lesquels il faut propager 
    */
    public function searchGroups($action,$group) {
        
        if ($action['propagationCurrent'] < $action['propagationMax']){
            
            if ($action['propagationCurrent'] == $places[0]){
                     
                $data = $this->Group->find('all',array(
                    'conditions' => array(
                        'Group.cities_id' => $group['Group']['cities_id']
                    )
                ));
            }
            else if ($action['propagationCurrent'] ==  $places[1]) {   

                $idPlaceDep = $this->getPlacesIdMax($group,'PlaceDepartment');
                $Cities = search($idPlaceDep,'PlaceDepartment');

                foreach($Cities as $City){                
                    $data .= $this->Group->find('all',array(
                        'conditions' => array(
                            'Group.cities_id' => $City['PlaceCity']['places_id']
                        )
                    ));
                }
            }
            else if ($action['propagationCurrent'] ==  $places[2]) {
                
                $idPlaceState = $this->getPlacesIdMax($group,'PlaceState');
                $Departements = search($idPlaceState,'PlaceState');
                
                foreach ($Departments as $Department){
                    
                    $Cities = search($Department['PlaceDepartment']['places_id'],'PlaceDepartment');
                    
                    foreach($Cities as $City){                
                        $data .= $this->Group->find('all',array(
                            'conditions' => array(
                                'Group.cities_id' => $City['PlaceCity']['places_id']
                            )
                        ));
                    }
                }

            }
            else if ($action['propagationCurrent'] ==  $places[3]) {
                
                $idPlaceCountry = $this->getPlacesIdMax($group,'PlaceCountry');
                $States =  search($idPlaceCountry,'PlaceCountry');

                foreach($States as $State){
                    
                    $Departements = search($State['PlaceState']['places_id'],'PlaceState');
                    
                    foreach ($Departments as $Department){
                        
                        $Cities = search($Department['PlaceDepartment']['places_id'],'PlaceDepartment');
                        
                        foreach($Cities as $City){      
                            
                            $data .= $this->Group->find('all',array(
                                'conditions' => array(
                                    'Group.cities_id' => $City['PlaceCity']['places_id']
                                )
                            ));
                        }
                    }
                }
            }
            else if ($action['propagationCurrent'] ==  $places[4]) {
               $Cities = $this->PlaceCity->find('all'); 
               foreach($Cities as $City){                
                    $data .= $this->Group->find('all',array(
                        'conditions' => array(
                            'Group.cities_id' => $City['PlaceCity']['places_id']
                        )
                    ));
                }
            }
        }
        else
            $data = NULL; 
        
        return $data;
    }
    
    /////////////////////////////////////////////////
    public function getNbPers($mongroup, $where = array(
        'world','country','state', 'department', 'city','group'
    )){
        $action['propagationCurrent'] = 'group';
        $action['propagationMax'] = $where;
        
        $ListGroup = $this->searchGroups($action,$mongroup);
        
        foreach($ListGroup as $group) {
            $data .= $this->User->UserGroup->find('count', array(
                'conditions' => array(
                    'UserGroup.groups_id' => $group['Group']['id']
                )
            ));
        }
        
        if ($data % 2 == 1){
            $data++;
        }
        
        return $data;
    }
    
}
?>
